package org.amse.korovkin.ciphering.view;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import org.amse.korovkin.ciphering.Main;
import org.amse.korovkin.ciphering.model.*;
import org.amse.korovkin.ciphering.view.AdjustementJDialog.AdjustementAction;

public class AttackJDialog extends JDialog implements IView{
    private static final String STOP_ICON_PATH = "org/amse/korovkin/ciphering/icons/stop-24.png";
    private static final String PAUSE_ICON_PATH = "org/amse/korovkin/ciphering/icons/pause-24.png";
    private static final String PLAY_ICON_PATH = "org/amse/korovkin/ciphering/icons/play-24.png";
    private static final ImageIcon PLAY_ICON= new ImageIcon
            (Main.class.getClassLoader().getResource(PLAY_ICON_PATH));
    private static final ImageIcon PAUSE_ICON= new ImageIcon
            (Main.class.getClassLoader().getResource(PAUSE_ICON_PATH));
    private static final ImageIcon STOP_ICON= new ImageIcon
            (Main.class.getClassLoader().getResource(STOP_ICON_PATH));
    public static final Object stopLock = new Object();
    public static final int SUCCESSFUL = 1;
    public static final int UNSUCCESSFUL = -1;
    private CipheringJFrame parent;
    private JProgressBar progressBar;
    private JLabel jl;
    private boolean pauseFlag;
    private JButton playPause = new JButton("Pause");
    private JButton stop = new JButton("Stop");
    private JButton adjustement = new JButton("Adjustement");
    private Dimension buttonSize = new Dimension(100,15);
    private int returnValue = UNSUCCESSFUL;
    private final int height = 150;
    private final int width = 500;
    
    public AttackJDialog(CipheringJFrame jFrame) {
        super(jFrame, "Attack File", true);
        parent = jFrame;
        setSize(new Dimension(width, height));
        setResizable(false);
        setLocation(parent.getX() + (parent.getWidth() - width) / 2
                , parent.getY() + parent.getHeight() - height);
        Container c = getContentPane();
        progressBar = new JProgressBar();
        pauseFlag = false;
        progressBar.setPreferredSize(new Dimension(402,20));
        progressBar.setBorder(new LineBorder(Color.BLACK, 1));
        c.setLayout(new FlowLayout());
        jl = new JLabel("Attack progress");
        jl.setPreferredSize(new Dimension(100,20));
        playPause.setPreferredSize(buttonSize);

        Box hBox = Box.createHorizontalBox();
        hBox.add(playPause);
        hBox.add(Box.createHorizontalStrut(5));
        hBox.add(stop);
        hBox.add(Box.createHorizontalStrut(5));
        hBox.add(adjustement);
        c.add(jl);
        c.add(progressBar);
        c.add(hBox);

        stop.setAction(new StopAction(this));
        stop.setText(null);
        playPause.setAction(new PlayPauseAction(this));
        playPause.setText(null);
        adjustement.setAction(new AdjustementAction(this));
        adjustement.setText(null);
    }
    public void setProgress(int percent) {
        progressBar.setValue(percent);
        if (percent == 100) {
            returnValue = SUCCESSFUL;
        }
    }
    public int getResult() {
        return returnValue;
    }
    public boolean getPauseFlag() {
        return pauseFlag;
    }
    public void setPause() {
        if (!pauseFlag) {
            playPause.doClick();
        }
    }
    public void pressStop() {
        IModel model = Model.getInstance();
        model.setStopFlag(true);
        playPause.setEnabled(false);
        stop.setEnabled(false);
    }
    public void pressPause() {
        if (!pauseFlag) {
            pauseFlag = true;
            IModel model = Model.getInstance();
            model.setPauseFlag(true);
            playPause.setIcon(PLAY_ICON);
            playPause.setToolTipText("Play");
        } else {
            pauseFlag = false;
            IModel model = Model.getInstance();
            model.setPauseFlag(false);
            synchronized (AttackJDialog.stopLock) {
                AttackJDialog.stopLock.notify();
            }
            playPause.setIcon(PAUSE_ICON);
            playPause.setToolTipText("Pause");
        }
    }

    public void update() {
        IModel model = Model.getInstance();
        int percent = model.getProgress();
        this.setProgress(percent);
        this.repaint();
    }
    public static class StopAction extends AbstractAction {
        AttackJDialog parent;
        public StopAction(AttackJDialog jFrame) {
            parent = jFrame;
            putValue(NAME, "Stop");
            putValue(SHORT_DESCRIPTION, "Stop Attack Process");
            putValue(LARGE_ICON_KEY, STOP_ICON);
        }
        public void actionPerformed(ActionEvent e) {
            parent.pressStop();
        }
    }
    public static class PlayPauseAction extends AbstractAction {
        AttackJDialog parent;
        public PlayPauseAction(AttackJDialog jFrame) {
            parent = jFrame;
            putValue(NAME, "Play");
            putValue(SHORT_DESCRIPTION, "Pause");
            putValue(LARGE_ICON_KEY, PAUSE_ICON);
        }
        public void actionPerformed(ActionEvent e) {
            parent.pressPause();
        }
    }
}
